package com.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.data.Common;
import com.data.LineData;
import com.data.MyData;

import java.util.ArrayList;

/**
 * 背景图
 * Created by White
 * Email：whitelaning@qq.com
 */
public class LineView extends View {
    private float xPoint = 0;// 原点X坐标
    private float yPoint = 0;// 原点Y坐标

    private Paint mGridPaint;
    private Paint mReferenceLinePaint;
    private Paint mNormalLinePaint;
    private Paint mDashedPaint;

    private int height;
    private int width;

    public LineView(Context context) {
        super(context);
    }

    public void initValue() {
        xPoint = 0;
        yPoint = 0;

        initPaint();
    }

    private void initPaint() {
        mGridPaint = new Paint();
        mGridPaint.setStyle(Paint.Style.STROKE);
        mGridPaint.setAntiAlias(true);
        mGridPaint.setColor(0xff999999);
        mGridPaint.setAlpha(255);

        mReferenceLinePaint = new Paint();
        mReferenceLinePaint.setStyle(Paint.Style.STROKE);
        mReferenceLinePaint.setAntiAlias(true);
        mReferenceLinePaint.setColor(0xff57D9BF);
        mReferenceLinePaint.setTextSize(35);
        mReferenceLinePaint.setStrokeWidth(1);
        mReferenceLinePaint.setPathEffect(new CornerPathEffect(50));

        mNormalLinePaint = new Paint();
        mNormalLinePaint.setStyle(Paint.Style.FILL);
        mNormalLinePaint.setAntiAlias(true);
        mNormalLinePaint.setColor(0xff57D9BF);
        mNormalLinePaint.setTextSize(35);
        mNormalLinePaint.setStrokeWidth(4);
        mNormalLinePaint.setPathEffect(new CornerPathEffect(50));

        mDashedPaint = new Paint();
        mDashedPaint.setStyle(Paint.Style.STROKE);
        mDashedPaint.setColor(0xffFF5400);
        mDashedPaint.setStrokeWidth(1);
        PathEffect effects = new DashPathEffect(new float[]{20, 10}, 2);
        mDashedPaint.setPathEffect(effects);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawGrid(canvas);
        // 画折线
        drawCurve(canvas);
        drawDashedLine(canvas);
    }

    private void drawDashedLine(Canvas canvas) {
        Path path = new Path();
        path.moveTo(indexX, 0);
        path.lineTo(indexX, height);
        canvas.drawPath(path, mDashedPaint);
    }

    private void drawGrid(Canvas canvas) {

        for (float lineHeight : Common.yScaleBeginsHeight) {
            canvas.drawLine(xPoint, lineHeight, xPoint + Common.xScaleTotalLength, lineHeight, mGridPaint);
        }

        for (int i = 0; i < Common.xScaleArrayLabel.size(); i++) {
            canvas.drawLine(xPoint + Common.xScaleEachLength * i, yPoint, xPoint + Common.xScaleEachLength * i, Common.yScaleTotalLength, mGridPaint);
        }
    }

    private void drawCurve(Canvas canvas) {
        for (MyData item : Common.LineDataSeries) {

            switch (Common.type) {
                case 0:
                    if (item.height != null && item.height.size() > 0) {
                        if (item.type == MyData.DataType.REFERENCE) {
                            drawReferenceLine(canvas, item.height, item.color, item.name);
                        } else {
                            drawNormalLine(canvas, item.height, item.color, item.name);
                        }
                    }
                    break;
                case 1:
                    if (item.weight != null && item.weight.size() > 0) {
                        if (item.type == MyData.DataType.REFERENCE) {
                            drawReferenceLine(canvas, item.weight, item.color, item.name);
                        } else {
                            drawNormalLine(canvas, item.weight, item.color, item.name);
                        }
                    }
                    break;
                case 2:
                    if (item.head != null && item.head.size() > 0) {
                        if (item.type == MyData.DataType.REFERENCE) {
                            drawReferenceLine(canvas, item.head, item.color, item.name);
                        } else {
                            drawNormalLine(canvas, item.head, item.color, item.name);
                        }
                    }
                    break;
            }
        }
    }

    private void drawNormalLine(Canvas canvas, ArrayList<LineData> array, int color, String title) {
        int radius = 10;

        mNormalLinePaint.setColor(color);

        for (int i = 0; i < array.size(); i++) {

            if (i < array.size() - 1) {
                LineData item1 = array.get(i);
                LineData item2 = array.get(i + 1);
                if (item1.x > 0 && item2.x > 0) {
                    canvas.drawLine(
                            item1.x,
                            item1.y,
                            item2.x,
                            item2.y,
                            mNormalLinePaint);
                }
            }

            canvas.drawCircle(array.get(i).x, array.get(i).y, radius, mNormalLinePaint);
        }
    }

    private void drawReferenceLine(Canvas canvas, ArrayList<LineData> array, int color, String title) {

        mReferenceLinePaint.setColor(color);

        for (int i = 0; i < array.size(); i++) {
            if (i < array.size() - 1) {
                LineData item1 = array.get(i);
                LineData item2 = array.get(i + 1);
                if (item1.x > 0 && item2.x > 0 && item1.y > 0 && item2.y > 0) {
                    canvas.drawLine(
                            item1.x,
                            item1.y,
                            item2.x,
                            item2.y,
                            mReferenceLinePaint);

//                    Rect mRect = new Rect();
//                    mReferenceLinePaint.getTextBounds(title, 0, title.length(), mRect);
//                    int width = mRect.width();//文本的宽度
//                    int height = mRect.height();//文本的高度
                }
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(Common.xScaleTotalLength, Common.yScaleTotalLength);
        height = getMeasuredHeight();
        width = getMeasuredWidth();
        indexX = Common.xScaleEachLength * 2;
    }

    public OnTouchListenerCurrentView mOnTouchListenerCurrentView;

    public interface OnTouchListenerCurrentView {
        boolean onTouch(View v, MotionEvent event, int indexX);
    }

    public void setOnTouchListenerCurrentView(OnTouchListenerCurrentView mOnTouchListenerCurrentView) {
        this.mOnTouchListenerCurrentView = mOnTouchListenerCurrentView;
    }

    private int indexX = 0;

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        indexX = l + Common.xScaleEachLength * 2;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Common.oldX = event.getX();
            Common.oldY = event.getY();
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE) {

            Common.parseX(Common.newX += Common.oldX - event.getX(), getWidth(), getHeight());
            indexX = Common.newX + Common.xScaleEachLength * 2;

            Common.oldX = event.getX();
            Common.oldY = event.getY();

            Log.e("TAG","Common.oldX = " + Common.oldX);
            Log.e("TAG","Common.oldY = " + Common.oldY);

            if (mOnTouchListenerCurrentView != null) {
                return mOnTouchListenerCurrentView.onTouch(this, event, indexX);
            }
        }

        return true;
    }
}
